import os
import json
import logging
from typing import Dict, Any
from .base_processor import BaseProcessor
from prompts import (
    PROMPT_SEEDING_SINGLE_PRODUCT_SYSTEM, PROMPT_SEEDING_SINGLE_PRODUCT_USER,
    PROMPT_SEEDING_UNBOXING_OUTFIT_SYSTEM, PROMPT_SEEDING_UNBOXING_OUTFIT_USER,
    PROMPT_SEEDING_VLOG_DAILY_SYSTEM, PROMPT_SEEDING_VLOG_DAILY_USER,
    PROMPT_SEEDING_GOODS_COLLECTION_SYSTEM, PROMPT_SEEDING_GOODS_COLLECTION_USER,
    PROMPT_SEEDING_OTHER_SYSTEM, PROMPT_SEEDING_OTHER_USER
)

# 获取当前模块的日志记录器
from app.utils.logger import get_logger

# 传入当前模块名，获取已配置好的日志器
logger = get_logger(__name__)


class SeedingProcessor(BaseProcessor):
    """种草类处理器（全面替换request为requirements）"""

    def __init__(self, volcano_client, additional_info=""):
        super().__init__(volcano_client)
        self.additional_info = additional_info
        logger.info(f"🎬 SeedingProcessor初始化完成，附加信息: {additional_info}")

    async def process(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        """处理种草类任务（删除request，仅保留requirements）"""
        logger.info("🚀 开始处理种草任务")

        # 提取入参（全面删除request，仅保留requirements）
        creator_style = inputs.get("creator_style", {})
        topic_result = inputs.get("topic_result", {})
        product_highlights = inputs.get("product_highlights", "")
        outline_advice = inputs.get("outline_advice", "")
        notice = inputs.get("notice", "")
        requirements = inputs.get("requirements", "")  # 仅保留requirements
        style_judgment_result = inputs.get("style_judgment_result", "")
        matched_direction = inputs.get("matched_direction", "seeding_其他")

        # 记录关键输入参数
        logger.info(f"📋 输入参数解析完成 - 匹配方向: {matched_direction}")
        logger.info(f"📝 需求文本长度: {len(requirements)} 字符")
        logger.info(f"🎯 产品亮点长度: {len(product_highlights)} 字符")
        logger.info(f"💡 大纲建议长度: {len(outline_advice)} 字符")
        logger.debug(f"创作者风格键值: {list(creator_style.keys()) if creator_style else '空'}")
        logger.debug(f"主题结果键值: {list(topic_result.keys()) if topic_result else '空'}")

        # 输入验证日志
        if not requirements:
            logger.warning("⚠️  requirements参数为空，可能会影响生成质量")
        if not product_highlights:
            logger.warning("⚠️  产品亮点为空")

        logger.info("🔄 开始调用模型处理...")

        # 调用模型（仅传递requirements）
        result = await self.select_and_call_model(
            matched_direction=matched_direction,
            creator_style=creator_style,
            topic_result=topic_result,
            product_highlights=product_highlights,
            outline_advice=outline_advice,
            notice=notice or self.additional_info,
            requirements=requirements,  # 仅传递requirements
            style_judgment_result=style_judgment_result
        )

        # 记录处理结果
        if "error" in result:
            logger.error(f"❌ 种草任务处理失败 - 方向: {matched_direction}, 错误: {result['error']}")
        else:
            logger.info(f"✅ 种草任务处理成功 - 方向: {matched_direction}")
            logger.debug(f"生成结果类型: {type(result)}")

        return {
            "content_type": "seeding",
            "matched_direction": matched_direction,
            "result": result,
            "additional_info": self.additional_info
        }

    async def select_and_call_model(
            self,
            matched_direction: str,
            creator_style: Dict[str, Any],
            topic_result: Dict[str, Any],
            product_highlights: str,
            outline_advice: str,
            notice: str,
            requirements: str,  # 仅保留requirements
            style_judgment_result: str
    ) -> Dict[str, Any]:
        """一对一方向调用（prompt中仅使用requirements）"""
        logger.info(f"🎯 选择模型处理方向: {matched_direction}")

        creator_style_str = json.dumps(creator_style, ensure_ascii=False, indent=2)
        topic_result_str = json.dumps(topic_result, ensure_ascii=False, indent=2)

        # 记录提示词构建信息
        logger.debug(f"构建提示词 - 创作者风格长度: {len(creator_style_str)}")
        logger.debug(f"构建提示词 - 主题结果长度: {len(topic_result_str)}")
        logger.debug(f"构建提示词 - 需求文本长度: {len(requirements)}")

        model_response = None

        try:
            # 1. 单品种草（仅用requirements）
            if matched_direction == "单品种草":
                logger.info("🌱 处理单品种草内容")
                user_prompt = PROMPT_SEEDING_SINGLE_PRODUCT_USER.format(
                    creator_style=creator_style_str,
                    topic_result=topic_result_str,
                    product_highlights=product_highlights,
                    outline_advice=outline_advice,
                    notice=notice,
                    requirements=requirements  # 替换原request
                )
                logger.debug(f"单品种草提示词长度: {len(user_prompt)}")
                model_response = await self.call_model(
                    system_prompt=PROMPT_SEEDING_SINGLE_PRODUCT_SYSTEM,
                    user_prompt=user_prompt
                )

            # 2. 开箱+穿搭（仅用requirements）
            elif matched_direction == "开箱+穿搭":
                logger.info("📦 处理开箱+穿搭内容")
                user_prompt = PROMPT_SEEDING_UNBOXING_OUTFIT_USER.format(
                    creator_style=creator_style_str,
                    topic_result=topic_result_str,
                    product_highlights=product_highlights,
                    outline_advice=outline_advice,
                    notice=notice,
                    requirements=requirements  # 替换原request
                )
                logger.debug(f"开箱+穿搭提示词长度: {len(user_prompt)}")
                model_response = await self.call_model(
                    system_prompt=PROMPT_SEEDING_UNBOXING_OUTFIT_SYSTEM,
                    user_prompt=user_prompt
                )

            # 3. vlog日常植入（仅用requirements）
            elif matched_direction == "vlog日常植入":
                logger.info("🎥 处理vlog日常植入内容")
                user_prompt = PROMPT_SEEDING_VLOG_DAILY_USER.format(
                    creator_style=creator_style_str,
                    topic_result=topic_result_str,
                    product_highlights=product_highlights,
                    outline_advice=outline_advice,
                    notice=notice,
                    requirements=requirements  # 替换原request
                )
                logger.debug(f"vlog日常植入提示词长度: {len(user_prompt)}")
                model_response = await self.call_model(
                    system_prompt=PROMPT_SEEDING_VLOG_DAILY_SYSTEM,
                    user_prompt=user_prompt
                )

            # 4. 好物合集（仅用requirements）
            elif matched_direction == "好物合集":
                logger.info("🛍️ 处理好物合集内容")
                user_prompt = PROMPT_SEEDING_GOODS_COLLECTION_USER.format(
                    creator_style=creator_style_str,
                    topic_result=topic_result_str,
                    product_highlights=product_highlights,
                    outline_advice=outline_advice,
                    notice=notice,
                    requirements=requirements  # 替换原request
                )
                logger.debug(f"好物合集提示词长度: {len(user_prompt)}")
                model_response = await self.call_model(
                    system_prompt=PROMPT_SEEDING_GOODS_COLLECTION_SYSTEM,
                    user_prompt=user_prompt
                )

            # 5. seeding_其他（仅用requirements）
            elif matched_direction == "seeding_其他":
                logger.info("📝 处理seeding_其他类型")
                user_prompt = PROMPT_SEEDING_OTHER_USER.format(
                    creator_style=creator_style_str,
                    topic_result=topic_result_str,
                    product_highlights=product_highlights,
                    outline_advice=outline_advice,
                    notice=notice,
                    requirements=requirements,  # 替换原request
                    style_judgment_result=style_judgment_result
                )
                logger.debug(f"seeding_其他提示词长度: {len(user_prompt)}")
                model_response = await self.call_model(
                    system_prompt=PROMPT_SEEDING_OTHER_SYSTEM,
                    user_prompt=user_prompt
                )

            else:
                logger.error(f"❌ 未知的种草方向: {matched_direction}")
                return {"error": f"未知的种草方向: {matched_direction}", "raw_inputs": locals()}

            # 记录模型响应信息
            if model_response:
                logger.info(f"🤖 模型调用成功，响应长度: {len(model_response)}")
                logger.debug(f"模型响应前500字符: {model_response[:500]}...")
            else:
                logger.error("❌ 模型响应为空")
                return {"error": "模型响应为空", "raw_inputs": locals()}

        except Exception as e:
            logger.error(f"💥 模型调用过程中发生异常: {str(e)}", exc_info=True)
            return {"error": f"模型调用异常: {str(e)}", "raw_inputs": locals()}

        # 解析JSON响应
        logger.info("🔄 开始解析模型响应JSON")
        parsed_result = self.parse_json_response(model_response)

        if "error" in parsed_result:
            logger.error(f"❌ JSON解析失败: {parsed_result['error']}")
            logger.debug(f"原始响应内容: {model_response}")
        else:
            logger.info("✅ JSON解析成功")
            logger.debug(
                f"解析结果键值: {list(parsed_result.keys()) if isinstance(parsed_result, dict) else '非字典类型'}")

        return parsed_result